package com.gaoxizhe.ccc.controller;/**
 * @author : Mr.Gao
 * @date :   2019/9/22 12:42
 */

import com.alibaba.fastjson.JSON;
import com.gaoxizhe.ccc.base.PassToken;
import com.gaoxizhe.ccc.base.RestEntityResponse;
import com.gaoxizhe.ccc.base.UserLoginToken;
import com.gaoxizhe.ccc.constant.ReturnBaseConstant;
import com.gaoxizhe.ccc.model.TestEntity;
import com.gaoxizhe.ccc.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @ClassName TestController
 * @Author Mr.Gao
 * @Date 2019/9/22 12:42
 * @Description TODO | Controller方法示例
 */
@Controller
@Slf4j
public class TestController {

    @Autowired
    private TestService testService;

    @GetMapping("/test")
    @UserLoginToken
    @ResponseBody
    public ResponseEntity<RestEntityResponse<TestEntity>> test(
            @RequestParam(required = false) String id ,
            HttpServletRequest RequestParam
    ) {

        HttpHeaders headers = new HttpHeaders();
        HttpStatus httpStatus = HttpStatus.OK;
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        RestEntityResponse<TestEntity> entityResponse = new RestEntityResponse<>();
        if (id == null || StringUtils.isEmpty(id)) {
            log.info("参数错误！！！");
            entityResponse.setCode(ReturnBaseConstant.PARAMETER_ERROR);
            entityResponse.setMsg("参数错误");
            return new ResponseEntity<>(entityResponse, headers, httpStatus);
        }
        Enumeration<String> headerNames = RequestParam.getHeaderNames();
        if (headerNames.hasMoreElements()) {
            String s = headerNames.nextElement();
            String header = RequestParam.getHeader(s);
            System.out.println(s + " --- "  + header);
        }

        TestEntity testEntity;
        testEntity = testService.testQuery(id);

        entityResponse.setCode(0);
        entityResponse.setMsg("success");
        entityResponse.setData(testEntity);
        log.info("接口返回参数：{}", JSON.toJSONString(entityResponse));
        return new ResponseEntity<>(entityResponse, headers, httpStatus);
    }

    @GetMapping("/")
    @PassToken
    public String index() {
        log.info("首页");
        return "index";
    }
}
